package com.kjs.web.handler;

import com.kjs.common.resultModel.JsonResult;
import com.kjs.execptionless.BusinessException;
import com.kjs.execptionless.ExceptionMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;


/**
 * 活动中心的异常统一处理
 * Created by zmk523@163.com on 2019/6/21 14:17
 */

@RestControllerAdvice
@Slf4j
public class ACExceptionHandler {

//    @ExceptionHandler(value = KjsAppException.class)
//    public JsonResult sykscHandle(KjsAppException e) {
//        e.error(log);
//        return new JsonResult(e.getErr(), e.getReason());
//    }
//
//    @ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
//    public JsonResult argumentMismatchHandle(MethodArgumentTypeMismatchException e) {
//        return new MobileResult<>(ResultConst.ERR_PARAM_INVALID, "无效参数");
//    }
//
//    @ExceptionHandler(value = MissingServletRequestParameterException.class)
//    public JsonResult missParameterHandle(MissingServletRequestParameterException e) {
//        return new MobileResult<>(ResultConst.ERR_PARAM_MISSING, "缺少参数");
//    }

    @ExceptionHandler(value = Exception.class)
    public JsonResult exceptionHandle(Exception e) {
        log.error("收到未知异常：{}", e);
        return ExceptionMsg.UnknownError;
    }

    /**
     * 主动throw的异常
     */
    @ExceptionHandler(BusinessException.class)
    public JsonResult handleUnProccessableServiceException(BusinessException e, HttpServletResponse response) {
        response.setStatus(e.getCode());
        return new JsonResult(e.getCode(), e.getMsg());
    }
}
